/*
 * antApp - jQuery plugin for  Hengde
 *         Base jQuery
 * 
 * Version: 0.1
 * Date: 2015-12-23
 * (c) Copyright 2015 AntDz. All Rights Reserved. 
 * 
 */

(function($) {
	
	var methods = {
		init: function(options) {
			var o = {
				t: 0,
				h: 0,
				u: '', //ajax地址
				c: 5, //ajax加载次数
				k: 0, //ajax参数
			};

			return this.each(function() {
				var $this = $(this);
				// 尝试去获取settings，如果不存在，则返回“undefined”
				var settings = $this.data("antApp");
				//console.log(settings);

				// 如果获取settings失败，则根据options和default创建它
				if (typeof settings === "undefined") {

					if (options) {
						$.extend(o, options);
					}
					$this.data("antApp", o);

				} else {
					// 如果我们获取了settings，则将它和options进行合并（这不是必须的，你可以选择不这样做）
					settings = $.extend({}, settings, options);
					// 如果你想每次都保存options，可以添加下面代码：
					$this.data("antApp", settings);
					// console.log($this.data);
				}

				//

			});
		},
		destroy: function() {
			return $(this).each(function() {
				var $this = $(this);
				// 执行代码
				// 删除元素对应的数据
				$this.removeData("antApp");
			});
		},
		initScroll: function() {
			var $this = $(this);
			var o = $this.data("antApp");
			$this.on('scroll', function() {
				o.h = $this[0].scrollHeight; //得到滚动条总长，赋给hght变量
				o.t = $this[0].scrollTop; //得到滚动条当前值，赋给top变量
			});
			$this.antApp('getContent', {
				id: 0
			});
//			$this.antApp('scrollD');
//			setInterval($this, $this.antApp('scrollD'), 2000);
		},
		
		 timer: function (action,context,time) {
            var _timer;
            if ($.isFunction(action)) {
                (function () {
                    _timer = setInterval(function () {
                        if (!action(context)) {
                            clearInterval(_timer);
                        }
                    }, time);
                })();
            }
        },
		getContent: function(m) {
			var $this = $(this);
			var o = $this.data("antApp");
			$.get(o.u, m, function(data) {
				$this.append(data);
				o.h = 0;
				o.t = 0;
			});
		}
	};


	$.fn.antApp = function(method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === "object" || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error("Method\"" + method + "\" does not exist on jQuery.antFlow");
			return this;
		}
		//		return method.apply(this, arguments);
	};
})(jQuery);